﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.Unity;
using SISLOG.BusinessLogic;
using SISLOG.BusinessEntities;

namespace SISLOG.Web
{
    public partial class RealizarOrdenCompra : BasePage<RealizarOrdenCompra>//System.Web.UI.Page
    {
        [Dependency]
        public IMaterialBL MaterialBL { get; set; }

        [Dependency]
        public IOrdenCompraBL OrdenCompraBL { get; set; }

        private const string VARMATERIAL = "material";
        private const string VARORDENCOMPRA = "ordenCompra";

        protected void Page_Load(object sender, EventArgs e)
        {
            GridView1.DataSource = MaterialBL.GetAllFromMaterial();
            GridView1.DataBind();                        
        }

        private void OrdenCompraDataBind(OrdenCompra ordencompra)
        {
            idOrdenCompraTextBox.Text = ordencompra.idOrdenCompra.ToString();
            fechaOrdenCompraTextBox.Text = ordencompra.fechaOrdenCompra.ToShortDateString();
            responsableTextBox.Text = ordencompra.responsable.ToString();

            gvDetalle.DataSource = ordencompra.DetalleOrdenCompra;
            gvDetalle.DataBind();

            Cache.Insert(VARORDENCOMPRA, ordencompra);
        }

        protected void botonBuscar_Click(object sender, EventArgs e)
        {
            List<Material> listaMaterial = new List<Material>();

            if (RadioButton1.Checked)
            {
                listaMaterial = MaterialBL.GetAllFromMaterialByCriterio(txtCriterio.Text);
            }
            else
            {
                listaMaterial = MaterialBL.GetAllFromMaterialByProveedor(txtCriterio.Text);
            }

            GridView1.DataSource = listaMaterial;//para cargar los datos en la grilla
            GridView1.DataBind();//mostrar los datos de la grilla
            ModalPopupExtender1.Show();
        }

        protected void btnCancelar_Click(object sender, EventArgs e)
        {
            Response.Redirect("RealizarOrdenCompra.aspx");
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ModalPopupExtender1.Show();

            var idMaterial = Int32.Parse(GridView1.SelectedValue.ToString());
            var material = MaterialBL.GetAllFromMaterialByIdMaterial(idMaterial);

            txtDescripcion.Text = material.descripcion;
            txtCantidad.Text = "1";

            Cache.Insert(VARMATERIAL, material);
        }

        protected void btnAgregar_Click(object sender, EventArgs e)
        {
            var material = Cache.Get(VARMATERIAL) as Material;
            var cantidad = Int32.Parse(txtCantidad.Text);

            DetalleOrdenCompra newDetalle = new DetalleOrdenCompra()
            {
                Material = material,
                idMaterial = material.idMaterial,
                cantidad = cantidad
            };
            var ordencompra = Cache.Get(VARMATERIAL) as OrdenCompra;
            ordencompra.DetalleOrdenCompra.Add(newDetalle);
            OrdenCompraDataBind(ordencompra);
        }
    }
}